package com.zl_huang.example.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zl_huang.example.dao.IScoreDaoHibernate;
import com.zl_huang.example.dao.ISubjectDaoHibernate;
import com.zl_huang.example.dao.IUserDaoHibernate;
import com.zl_huang.example.model.Score;
import com.zl_huang.example.model.Subject;
import com.zl_huang.example.model.User;
import com.zl_huang.example.model.vo.ScoreVO;
import com.zl_huang.example.model.vo.SubjectVO;
import com.zl_huang.example.model.vo.UserVO;
@Service
public class StudentServiceImpl implements IStudentService {
	@Autowired
	private IUserDaoHibernate userDaoHibernate;
	@Autowired
	private ISubjectDaoHibernate  subjectDaoHibernate;
	@Autowired
	private IScoreDaoHibernate scoreDaoHibernate;

	@Override
	public List<SubjectVO> getSubjectList(User user) throws Exception {
		List<SubjectVO> resultList = new ArrayList<SubjectVO>();
		//通过user_id查询该同学对应的考试科目列表
		List<Subject> list = subjectDaoHibernate.getAllSubjectByUserId(user.getUser_id());
		for(Subject subject : list){
			System.out.println("该用户要考的科目"+subject.getName());
			SubjectVO subjectVO = new SubjectVO();
			BeanUtils.copyProperties(subject, subjectVO);
			//读取科目对应的成绩，可能是空的
			Score score = scoreDaoHibernate.getScoreByUserIdAndSubId(user.getUser_id(), subject.getSub_id());
			
			if(score != null){
				System.out.println("该科目对应的考试成绩是"+score.getScore());
				ScoreVO scoreVO = new ScoreVO();
				BeanUtils.copyProperties(score, scoreVO);
				//计算剩余的考试时间
				Calendar startTime = Calendar.getInstance();
				startTime.setTime(scoreVO.getExam_time());
				Calendar currentTime = Calendar.getInstance();
				currentTime.setTime(new Date());
				long startSecond = startTime.getTimeInMillis();
				long currentSecond = currentTime.getTimeInMillis();	
				long totalSecond = subject.getTotal_time()*60;
				scoreVO.setRemain_exam_time(totalSecond - (currentSecond-startSecond) / 1000);
				subjectVO.setScoreVO(scoreVO);
			}
			resultList.add(subjectVO);
		}
		
		return resultList;
	}

	@Override
	public ScoreVO getScoreById(Integer scoreId) throws Exception {
		//通过成绩id查找成绩表
		ScoreVO scoreVO = null;
		Score score = scoreDaoHibernate.getScoreById(scoreId);
		if(score != null){
			scoreVO = new ScoreVO();
			BeanUtils.copyProperties(score, scoreVO);
			scoreVO.setExam_time_Stirng(new SimpleDateFormat("yyyy-MM-dd").format(scoreVO.getExam_time()));
			User user = userDaoHibernate.getUserById(score.getUser_id());
			if(user != null){
				UserVO userVO = new UserVO();
				BeanUtils.copyProperties(user, userVO);
				scoreVO.setUserVO(userVO);
			}
			Subject subject = subjectDaoHibernate.getSubjectById(score.getSubject_id());
			if(subject != null){
				SubjectVO subjectVO = new SubjectVO();
				BeanUtils.copyProperties(subject, subjectVO);
				scoreVO.setSubjectVO(subjectVO);
			}
		}
		return scoreVO;
	}

}
